<?php



class ObjectController extends Zend_Controller_Action
{
	public function init()
	{
		$title ="add an object";
		$this->view->__set("headtitle", $title);
		 
		
	}
	
	public function indexAction()
	{
		
	}

	
	/**
	 * Upload the address from the form to the data base.
	 * 
	 * @param unknown_type $form
	 * @return Application_Model_Address
	 */
// 	private function uploadAddress($form){
// 		$addressValues = array(
// 				'text'			=> $form->getValue('address'),
// 				'zipCode'		=> $form->getValue('zipCode'),
// 				'city'			=> $form->getValue('city'),
// 				'country'		=> $form->getValue('country'),
// 		);
// 		$address = new Application_Model_Address($addressValues);
// 		$addressMapper = new Application_Model_AddressMapper();
// 		$id_address = $addressMapper->save($address);
// 		$address->setAddressId($id_address);
// 		return $address;
// 	}
	
	/**
	 * Function that rename the image and upload it into the images's folder
	 * and construct a list of Application model image to return
	 * @param unknown_type $imageform
	 * @return Application_Model_Image
	 */
	private function uploadImage($imageform){
		//Renommage de l'image avant l'upload
		$images = array();
		$imageform->receive();
		$files = $imageform->getFileInfo();
		foreach ($files as $file => $info) {
			if ($imageform->isUploaded($file)) {
			
				$originalFilename = pathinfo($imageform->getFileName($file));
				$newName = rand(1,100) . time() . $originalFilename['basename'];
				//$imageform->addFilter('Rename', array( 'target' =>'images/'.$newName, 'overwrite' => true) );
				//if(file_exists(APPLICATION_PATH.'/images/'.$originalFilename['basename']))
				rename(PUBLIC_PATH.'/img/object/'.$originalFilename['basename'], PUBLIC_PATH.'/img/object/'.$newName);
					
				$imageValues = array(
						'url'		=> $newName,
						'created'	=> date('Y-m-d H:i:s'),
				);
				$image = new Application_Model_Image($imageValues);
					
				$images[] = $image;
			}
		}
		return $images;
	}
	
	/**
	 * Action to add or update a house in the data base from an HTML form
	 */
	public function houseAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_House('house');
			$house = new Application_Model_House();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
			
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
							
							
							$house->setHouseId($object->getHouseId());
							$house->setRealestateId($object->getRealestateId());
							$house->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
					
					$images = $this->uploadImage($form->image);
					
					$imagesarray = array(
							'images' => $images,
							);
					
					$house->setOptions(array_merge($form->getValues(),$imagesarray));
					
					$housemapper  = new Application_Model_HouseMapper();
					$id_house = $housemapper->save($house);
					$house = $housemapper->find($id_house, $house);
					$id_house_object = $house->getObjectId();
					$this->addOffer($userConnected,$id_house_object, $id_house, $house, $housemapper);
					$this->_helper->redirector($id_house.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($house->getName()), 'house', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
			
			$this->_helper->redirector ( 'index', 'index' );
		}
		
	}
	
	/**
	 * Action to add or update an apartment in the data base from an HTML form
	 */
	public function apartmentAction() {
		
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Apartment('apartment');
			$apartment = new Application_Model_Apartment();
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
								
							$apartment->setApartmentId($object->getApartmentId());
							$apartment->setRealestateId($object->getRealestateId());
							$apartment->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
					$images = $this->uploadImage($form->image);
					
					$imagesarray = array(
							'images' => $images,
					);
					
					$apartment->setOptions(array_merge($form->getValues(),$imagesarray));
					
					$apartmentmapper  = new Application_Model_ApartmentMapper();
					$id_appartement = $apartmentmapper->save($apartment);
					$apartment->setApartmentId($id_appartement);
					$apartment = $apartmentmapper->find($id_appartement, $apartment);
					$id_apartment_object = $apartment->getObjectId();
					$this->addOffer($userConnected,$id_apartment_object, $id_appartement, $apartment, $apartmentmapper); 					
					$this->_helper->redirector($id_appartement.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($apartment->getName()), 'apartment', 'view');
				}
			}
			$this->view->form = $form;
		}else{
				
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	
//TODO handle the array of images
// 	private function uploadImage($imageform){
// 		$images = array();
// 		foreach($imageform as $imagefo)
// 		{
// 			//Renommage de l'image avant l'upload
// 			$originalFilename = pathinfo($imagefo->getFileName());
// 			$newName = rand(1,100) . time() . $originalFilename['basename'];
// 			$imagefo->addFilter('Rename', $newName);
	
// 			$imageValues = array(
// 					'url'		=> 'images/'.$newName,
// 					'created'	=> date('Y-m-d H:i:s'),
// 			);
// 			$image = new Application_Model_Image($imageValues);
// 			$images[] = $image;
// 		}
// 		return $images;
// 	}
//TODO to delete
// 	private function addAssocIbjImg($id_image,$id_object){
// 		$assocValues = array(
// 				'imageId'	=> $id_image,
// 				'objectId'	=> $id_object,
// 		);
// 		$assocIO = new Application_Model_AssocObjImg($assocValues);
// 		$assocIOMapper = new Application_Model_AssocObjImgMapper();
// 		$assocIOMapper->save($assocIO);
// 	}
	
	/**
	 * Action to add a car in the data base from an HTML form
	 */
	public function carAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Car('car');
			$car = new Application_Model_Car();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
				
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
								
							$car->setCarId($object->getCarId());
							$car->setVehicleId($object->getVehicleId());
							$car->setProductId($object->getProductId());
							$car->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
	
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
					
					
					$images = $this->uploadImage($form->image);
					$fimages = array('images' => $images);
					$car->setOptions(array_merge($form->getValues(),$fimages));
					
					$carmapper  = new Application_Model_CarMapper();
					$id_car = $carmapper->save($car);
					$car = $carmapper->find($id_car, $car);
					$id_car_object = $car->getObjectId();
					$this->addOffer($userConnected,$id_car_object, $id_car, $car, $carmapper);
					$this->_helper->redirector($id_car.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($car->getName()),'car', 'view');
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	/**
	 * Action to add a car in the data base from an HTML form
	 */
// 	public function car1Action() {
	
// 		$auth = Zend_Auth::getInstance ();
// 		if ($auth->hasIdentity ()) {
// 			$form = new Application_Form_File('car');
	
// 			if($this->_request->isPost()){
// 				$formData = $this->getRequest()->getPost();
// 				if($form->isValid($formData))
// 				{
// 					$names = array();
// 					$adapter = new Zend_File_Transfer_Adapter_Http();
					
// 					$files = $adapter->getFileInfo();
// 					foreach ($files as $file => $info) {
// 						$name = $adapter->getFileName($file);
// 						$names[] = $name;
// 						$originalFilename = pathinfo($name);
// 						$newName = rand(1,100) . $originalFilename['basename'];
// 						$adapter->addFilter('Rename', APPLICATION_PATH.'/images/'.$newName, $file['tmp_name']);
// 						$adapter->receive($file['tmp_name']);
// 					}
// 					$this->view->error = var_dump(APPLICATION_PATH);
// 				}
// 			}
// 			$this->view->form = $form;
// 		}else{
	
// 			$this->_helper->redirector ( 'index', 'index' );
// 		}
	
// 	}
	
	/**
	 * Action to add a motorcycle in the data base from an HTML form
	 */
	public function motorcycleAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Motorcycle('motorcycle');
			$motorcycle = new Application_Model_Motorcycle();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
				
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
								
							$motorcycle->setMotorcycleId($object->getMotorcycleId());
							$motorcycle->setVehicleId($object->getVehicleId());
							$motorcycle->setProductId($object->getProductId());
							$motorcycle->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
	
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{

					$images = $this->uploadImage($form->image);
					$fimages = array('images' => $images);
					$motorcycle->setOptions(array_merge($form->getValues(),$fimages));
						
						
					$motorcyclemapper  = new Application_Model_MotorcycleMapper();
					$id_motor = $motorcyclemapper->save($motorcycle);
					$motorcycle = $motorcyclemapper->find($id_motor, $motorcycle);
					$id_motor_object = $motorcycle->getObjectId();
					$this->addOffer($userConnected,$id_motor_object, $id_motor, $motorcycle, $motorcyclemapper);
					$this->_helper->redirector($id_motor.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($motorcycle->getName()),'motorcycle', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	/**
	 * Action to add a land in the data base from an HTML form
	 */
	public function landAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Land('land');
			$land = new Application_Model_Land();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
			
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
							
							$land->setLandId($object->getLandId());
							$land->setRealestateId($object->getRealestateId());
							$land->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
					$images = $this->uploadImage($form->image);
						
					$imagesarray = array(
							'images' => $images,
					);
					
					$land->setOptions(array_merge($form->getValues(),$imagesarray));
					
					$landmapper  = new Application_Model_LandMapper();
					$id_land = $landmapper->save($land);
					$land = $landmapper->find($id_land, $land);
					$id_land_object = $land->getObjectId();
					$this->addOffer($userConnected,$id_land_object, $id_land, $land, $landmapper);
					$this->_helper->redirector($id_land.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($land->getName()), 'land', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
				
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	/**
	 * function that save the offer in the data base 
	 * @param Application_Model_User $user
	 * @param int $id_class
	 * @param Application_Model_Object $object
	 * @param Applciation_Model_ObjectMapper $objectMapper
	 */
	public function addOffer($user,$id_object,$id_class,$object,$objectMapper){
		$object = $objectMapper->find($id_class, $object);
		$classe = get_class($object);
		$classe = explode("_",$classe);
		$classe = $classe[2];
		$offerMapper = new Application_Model_OfferMapper();
		$where = array( 'id_classe = ?' => $id_class,
						'id_object = ?' => $id_object,
						'id_user = ?'	=> $user->getUserId());
		
		$offer = $offerMapper->findWhere($where);
		if($offer == null)
			$offer = new Application_Model_Offer();
		$offer->setObject($object);
		$offer->setUser($user);
		$offer->setClasse($classe);
		$offer->setId_classe($id_class);
		$offerMapper->save($offer);
		
	}
	
	/**
	 * Action to add a parking in the data base from an HTML form
	 */
	public function parkingAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Parking('parking');
			$parking = new Application_Model_Parking();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
				
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
								
							$parking->setParkingId($object->getParkingId());
							$parking->setRealestateId($object->getRealestateId());
							$parking->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
	
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
					$images = $this->uploadImage($form->image);
						
					$imagesarray = array(
							'images' => $images,
					);
					
					$parking->setOptions(array_merge($form->getValues(),$imagesarray));
					
					$parkingmapper  = new Application_Model_ParkingMapper();
					$id_parking = $parkingmapper->save($parking);
					$parking = $parkingmapper->find($id_parking, $parking);
					$id_parking_object = $parking->getObjectId();
					$this->addOffer($userConnected,$id_parking_object, $id_parking, $parking, $parkingmapper);
					$this->_helper->redirector($id_parking.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($parking->getName()),'parking', 'view');
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a book in the data base from an HTML form
	 */
	public function bookAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Book('book');
			$book = new Application_Model_Book();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$book->setBookId($object->getBookId());
							$book->setRealestateId($object->getRealestateId());
							$book->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$book->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$bookmapper  = new Application_Model_BookMapper();
					$id_book = $bookmapper->save($book);
					$book = $bookmapper->find($id_book, $book);
					$id_book_object = $book->getObjectId();
					$this->addOffer($userConnected,$id_book_object, $id_book, $book, $bookmapper);
					$this->_helper->redirector($id_book.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($book->getName()), 'book', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a other in the data base from an HTML form
	 */
	public function otherAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Other('other');
			$other = new Application_Model_Other();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$other->setOtherId($object->getOtherId());
							$other->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$other->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$othermapper  = new Application_Model_OtherMapper();
					$id_other = $othermapper->save($other);
					$other = $othermapper->find($id_other, $other);
					$id_other_object = $other->getObjectId();
					$this->addOffer($userConnected,$id_other_object, $id_other, $other, $othermapper);
					$this->_helper->redirector($id_other.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($other->getName()), 'other', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a desktop in the data base from an HTML form
	 */
	public function desktopAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Desktop('desktop');
			$desktop = new Application_Model_Desktop();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$desktop->setDesktopId($object->getDesktopId());
							$desktop->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$desktop->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$desktopmapper  = new Application_Model_DesktopMapper();
					$id_desktop = $desktopmapper->save($desktop);
					$desktop = $desktopmapper->find($id_desktop, $desktop);
					$id_desktop_object = $desktop->getObjectId();
					$this->addOffer($userConnected,$id_desktop_object, $id_desktop, $desktop, $desktopmapper);
					$this->_helper->redirector($id_desktop.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($desktop->getName()), 'desktop', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a laptop in the data base from an HTML form
	 */
	public function laptopAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Laptop('laptop');
			$laptop = new Application_Model_Laptop();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$laptop->setLaptopId($object->getLaptopId());
							$laptop->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$laptop->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$laptopmapper  = new Application_Model_LaptopMapper();
					$id_laptop = $laptopmapper->save($laptop);
					$laptop = $laptopmapper->find($id_laptop, $laptop);
					$id_laptop_object = $laptop->getObjectId();
					$this->addOffer($userConnected,$id_laptop_object, $id_laptop, $laptop, $laptopmapper);
					$this->_helper->redirector($id_laptop.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($laptop->getName()), 'laptop', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a tv in the data base from an HTML form
	 */
	public function tvAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Tv('tv');
			$tv = new Application_Model_Tv();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$tv->setTvId($object->getTvId());
							$tv->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$tv->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$tvmapper  = new Application_Model_TvMapper();
					$id_tv = $tvmapper->save($tv);
					$tv = $tvmapper->find($id_tv, $tv);
					$id_tv_object = $tv->getObjectId();
					$this->addOffer($userConnected,$id_tv_object, $id_tv, $tv, $tvmapper);
					$this->_helper->redirector($id_tv.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($tv->getName()), 'tv', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a phone in the data base from an HTML form
	 */
	public function phoneAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Phone('phone');
			$phone = new Application_Model_Phone();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$phone->setPhoneId($object->getPhoneId());
							$phone->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$phone->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$phonemapper  = new Application_Model_PhoneMapper();
					$id_phone = $phonemapper->save($phone);
					$phone = $phonemapper->find($id_phone, $phone);
					$id_phone_object = $phone->getObjectId();
					$this->addOffer($userConnected,$id_phone_object, $id_phone, $phone, $phonemapper);
					$this->_helper->redirector($id_phone.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($phone->getName()), 'phone', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
	
	
	/**
	 * Action to add or update a clothing in the data base from an HTML form
	 */
	public function clothingAction() {
	
		// the get parameters
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$form = new Application_Form_Clothing('clothing');
			$clothing = new Application_Model_Clothing();
			//get the user connected
			$userConnected = new Application_Model_User();
			$userMapper = new Application_Model_UserMapper();
			$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
	
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						if($userConnected == $objectOwner )
						{
							$elementImage = $form->getElement('image');
							$elementImage->setRequired(false);
							$form->populate($object->getAttribs());
	
	
							$clothing->setClothingId($object->getClothingId());
							$clothing->setObjectId($object->getObjectId());
						}
						else{
							$this->view->alert = 'cet object ne vous appartient pas';
						}
					}
				}
			}
			if($this->_request->isPost()){
				$formData = $this->getRequest()->getPost();
				if($form->isValid($formData))
				{
	
					$images = $this->uploadImage($form->image);
	
					$imagesarray = array(
							'images' => $images,
					);
	
					$clothing->setOptions(array_merge($form->getValues(),$imagesarray));
	
					$clothingmapper  = new Application_Model_ClothingMapper();
					$id_clothing = $clothingmapper->save($clothing);
					$clothing = $clothingmapper->find($id_clothing, $clothing);
					$id_clothing_object = $clothing->getObjectId();
					$this->addOffer($userConnected,$id_clothing_object, $id_clothing, $clothing, $clothingmapper);
					$this->_helper->redirector($id_clothing.'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($clothing->getName()), 'clothing', 'view' );
				}
			}
			$this->view->form = $form;
		}else{
	
			$this->_helper->redirector ( 'index', 'index' );
		}
	
	}
}